package com.zsrd.lessons.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsrd.core.response.ResponseSupport;
import com.zsrd.lessons.domain.dto.TsLessonsAddDto;
import com.zsrd.lessons.domain.dto.TsLessonsHistoryDto;
import com.zsrd.lessons.domain.dto.TsLessonsListDto;
import com.zsrd.lessons.domain.dto.TsLessonsUpdateDto;
import com.zsrd.lessons.service.TsLessonsHistoryService;
import com.zsrd.lessons.service.TsLessonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/lessons")
public class LessonsController {
    @Autowired
    private TsLessonsService tsLessonsService;
    @Autowired
    private TsLessonsHistoryService tsLessonsHistoryService;


    @RequestMapping("/getLessonList")
    public ResponseSupport getLessonList(@RequestParam(required = false) String keyword,
                                         @RequestParam String orgNo,
                                         @RequestParam Long current,
                                         @RequestParam Long size) {
        try {
            IPage<TsLessonsListDto> list = tsLessonsService.getLessonList( keyword, orgNo, current, size);
            return ResponseSupport.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    @RequestMapping("/getLessonById")
    public ResponseSupport getLessonById(@RequestParam Long id, @RequestParam String loginId) {
        try {
            Map<String, Object> m = tsLessonsService.getLessonById(loginId, id);
            return ResponseSupport.success(m);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    /**
     * 课程管理-访问历史
     *
     * @param id
     * @return
     */
    @RequestMapping("/getReadPeopleList")
    public ResponseSupport getReadPeopleList(Long id, Long current, Long size) {
        try {
            IPage<TsLessonsHistoryDto> page = tsLessonsHistoryService.getReadPeopleList(id, current, size);
            return ResponseSupport.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    @RequestMapping("/addLesson")
//    public R addLesson(@Valid @RequestBody TsLessonsAddDto tsLessons) {
    public ResponseSupport addLesson(@RequestBody TsLessonsAddDto tsLessons) {
        try {
            tsLessonsService.addLesson(tsLessons);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }

    @RequestMapping("/updateLesson")
//    public R updateLesson(@Valid @RequestBody TsLessonsUpdateDto tsLessons) {
    public ResponseSupport updateLesson( @RequestBody TsLessonsUpdateDto tsLessons) {
        try {
            tsLessonsService.updateLesson(tsLessons);
            return ResponseSupport.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }


    @RequestMapping("/deleteLesson")
    public ResponseSupport deleteLesson(@RequestParam Long id) {
        try {
            tsLessonsService.deleteLesson(id);
            return ResponseSupport.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseSupport.fail(e.getMessage());
        }
    }





}

